package cn.liumouren.tool.security.autoconfigure.config;

import cn.liumouren.tool.security.autoconfigure.SecurityProperties;
import cn.liumouren.tool.security.autoconfigure.component.filter.CheckTokenFilter;
import cn.liumouren.tool.security.core.DlFilter;
import cn.liumouren.tool.security.core.TokenProcessor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Daniel Liu
 * @date 2021/3/11 12:37
 */
@Configuration
@ConditionalOnProperty(value = "dl.security.enable", havingValue = "true", matchIfMissing = true)
public class FilterConfiguration {

    private final SecurityProperties properties;
    private final TokenProcessor tokenProcessor;

    public FilterConfiguration(SecurityProperties properties, TokenProcessor tokenProcessor) {
        this.properties = properties;
        this.tokenProcessor = tokenProcessor;
    }

    @Bean
    @ConditionalOnMissingBean(CheckTokenFilter.class)
    public DlFilter checkTokenFilter() {
        DlFilter dlFilter = new CheckTokenFilter(properties, tokenProcessor);
        dlFilter.addPatterns("/**");
        return dlFilter;
    }

}
